﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using ServeNHL.Helpers;


namespace ServeNHL.Models
{
    
    [MetadataType(typeof(eventMetadata))]
    public partial class EVENT
    {

        /// <summary>
        /// Given an EventViewModel Saves or updates event information
        /// </summary>
        /// <param name="events"></param>
        /// <returns>Event ID</returns>
        public int SaveEvent(EVENT eventItem)
        {
            int eventID = 0;

            if (eventItem.EVENT_ID > 0)
            {
                UpdateEvent(eventItem);
            }
            else
            {
                InsertEvent(eventItem);
            }

            eventID = eventItem.EVENT_ID;

            return eventID;

        } //end of  public int SaveEvent(eventViewModel events)


        /// <summary>
        /// Adds an event to the database
        /// </summary>
        /// <param name="events"></param>
        /// <returns>event ID</returns>
        public int InsertEvent(EVENT events)
        {
            int eventID = 0;

            using (var dbconn = new fclick_dbEntities())
            {

                var tempEvent = new EVENT
                {
                    NAME = events.NAME,
                    EVENT_DESC = events.EVENT_DESC,
                    EVENT_CAT_ID = events.EVENT_CAT_ID,
                    OWNER_ID = events.OWNER_ID,
                    START_DATE = events.START_DATE,
                    END_DATE = events.END_DATE,
                    STATUS = events.STATUS,
                    NUM_GUESTS = events.NUM_GUESTS,
                    MIN_PARTICIPANTS = events.MIN_PARTICIPANTS,
                    MAX_PARTICIPANTS = events.MAX_PARTICIPANTS,
                    NEED_FLAG = events.NEED_FLAG,
                    VCL_ID = events.VCL_ID,
                    LAST_MOD_DATE = Helpers.createDate.getCreateDate(),
                    CREATED_DATE = Helpers.createDate.getCreateDate()
                };

                dbconn.EVENTs.Add(tempEvent);
                dbconn.SaveChanges();

                eventID = tempEvent.EVENT_ID;
                
            }
           return eventID;

        } //end of public int InsertEvent(EVENT events)
        
        
        /// <summary>
        /// Updates event in database. called by SaveEvent
        /// </summary>
        /// <param name="events"></param>
        /// <returns>eventID</returns>
       public int UpdateEvent(EVENT events)
        {
            int eventID = 0;

            using (var dbconn = new fclick_dbEntities())
            {
                var tempEvent = new EVENT();

                if (!String.IsNullOrEmpty(events.NAME))
                {
                    tempEvent.NAME = events.NAME;
                }

                if (!String.IsNullOrEmpty(events.EVENT_DESC))
                {
                    tempEvent.EVENT_DESC = events.EVENT_DESC;
                }

                if (events.EVENT_CAT_ID >= 0)
                {
                    tempEvent.EVENT_CAT_ID = events.EVENT_CAT_ID;
                }
                       
                if (!String.IsNullOrEmpty(events.START_DATE.ToString()))
                {
                    tempEvent.START_DATE = events.START_DATE;
                }                   
                    
                if (!String.IsNullOrEmpty(events.END_DATE.ToString()))
                {
                    tempEvent.END_DATE = events.END_DATE;
                }
                         
                if (events.NUM_GUESTS >= 0)
                {
                    tempEvent.NUM_GUESTS = events.NUM_GUESTS;
                }

                if (events.MIN_PARTICIPANTS >= 0)
                {
                    tempEvent.MIN_PARTICIPANTS = events.MIN_PARTICIPANTS;
                }

                if (events.MAX_PARTICIPANTS >= 0)
                {
                    tempEvent.MAX_PARTICIPANTS = events.MAX_PARTICIPANTS;
                }

                    tempEvent.NEED_FLAG = events.NEED_FLAG;
             
                if (events.VCL_ID >= 0)
                {
                    tempEvent.VCL_ID = events.VCL_ID;
                }

                dbconn.EVENTs.Add(tempEvent);
                dbconn.SaveChanges();

                eventID = tempEvent.EVENT_ID;
            }//end of using (var dbconn = new fclick_dbEntities())

            return eventID;
        }//end of UpdateEvent(EVENT events)

    }//end of  public partial class @event
}